package com.jiyun.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.dto.ChoujiangResultDto;
import com.jiyun.entity.ChoujiangReuslt;
import com.jiyun.entity.Goods;
import com.jiyun.entity.Qualification;
import com.jiyun.entity.ZhongjiangRecord;
import com.jiyun.mapper.ChoujiangReusltMapper;
import com.jiyun.mapper.GoodsMapper;
import com.jiyun.mapper.QualificationMapper;
import com.jiyun.mapper.ZhongjiangRecordMapper;
import com.jiyun.service.IChoujiangReusltService;
import org.apache.dubbo.config.annotation.DubboService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-02-20
 */
@DubboService
public class ChoujiangReusltServiceImpl extends ServiceImpl<ChoujiangReusltMapper, ChoujiangReuslt> implements IChoujiangReusltService {


    @Autowired
    ChoujiangReusltMapper choujiangReusltMapper;
    @Autowired
    RedissonClient redissonClient;

    @Autowired
    ZhongjiangRecordMapper zhongjiangRecordMapper;

    @Autowired
    QualificationMapper qualificationMapper;

    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public String submitRecord(ChoujiangResultDto choujiangResultDto) {

        String result = "OK";

        /**
         * 0、先得检查库存是否充足；需要使用分布式锁
         * 1、保存抽奖记录
         * 2、如果中奖，需要保存中奖记录
         * 3、消耗掉当前的抽奖资格
         * 4、如果中奖，需要库存扣减
         */


        RLock myLock = redissonClient.getLock("MY_LOCK");
        myLock.lock();//开始锁定
        try {

            //未中奖

            if(!"中奖".equals(choujiangResultDto.getState())){
                //保存抽奖结果
                ChoujiangReuslt choujiangReuslt = new ChoujiangReuslt();
                BeanUtils.copyProperties(choujiangResultDto,choujiangReuslt);
                choujiangReuslt.setState("未中奖");
                choujiangReusltMapper.insert(choujiangReuslt);
                //消耗抽奖资格
                //消耗掉当前的抽奖资格
                Qualification qualification = new Qualification();
                qualification.setId(choujiangResultDto.getQid());
                qualification.setState("已使用");
                qualificationMapper.updateById(qualification);
                return result;
            }

            //中奖过程

            //0、先得检查库存是否充足；需要使用分布式锁
            Goods goods = goodsMapper.selectById(choujiangResultDto.getGid());
            if (goods.getStock()==0) {
                result = "你来晚一步，奖品被抢光啦";
                //进行后续操作

                /**
                 * 需要保存抽奖记录
                 * 消耗抽奖资格
                 */
                //1、保存抽奖记录
                ChoujiangReuslt choujiangReuslt = new ChoujiangReuslt();
                BeanUtils.copyProperties(choujiangResultDto,choujiangReuslt);
                choujiangReuslt.setState("未中奖");
                choujiangReusltMapper.insert(choujiangReuslt);


                //3、消耗掉当前的抽奖资格
                Qualification qualification = new Qualification();
                qualification.setId(choujiangResultDto.getQid());
                qualification.setState("已使用");
                qualificationMapper.updateById(qualification);

                return result;
            }
            //1、保存抽奖记录
            ChoujiangReuslt choujiangReuslt = new ChoujiangReuslt();
            BeanUtils.copyProperties(choujiangResultDto,choujiangReuslt);
            choujiangReusltMapper.insert(choujiangReuslt);
            //2、如果中奖，需要保存中奖记录
            if("中奖".equals(choujiangResultDto.getState())){

                ZhongjiangRecord zhongjiangRecord = new ZhongjiangRecord();

                BeanUtils.copyProperties(choujiangResultDto,zhongjiangRecord);

                zhongjiangRecord.setRid(choujiangReuslt.getId());

                zhongjiangRecordMapper.insert(zhongjiangRecord);

                //4、如果中奖，需要库存扣减
                goodsMapper.koujiankucun(choujiangResultDto.getGid());
            }
            //3、消耗掉当前的抽奖资格
            Qualification qualification = new Qualification();
            qualification.setId(choujiangResultDto.getQid());
            qualification.setState("已使用");
            qualificationMapper.updateById(qualification);

        }finally {
            myLock.unlock();//解锁，释放锁
        }
        return result;
    }
}
